<?php

class LoginAction extends CommonAction {
	//后台首页
    public function index(){
    	
        $this->display();
    }
    //检验登陆
    public function checkLogin(){
    	$userid = $this->_post('login_name');
    	$password = $this->_post('login_pass');
    	
    	$userModel = M("User");
		$user = $userModel->where(array('admin_id'=>$userid))->select();
		
		if(!empty($user) && ($user[0]['admin_pass']==sha1($password)) && ($user[0]['admin_ok']==2)){
			session('userid',$userid);
			session('lang',$user[0]['lang']);
			
			$data['admin_login'] = $user[0]['admin_login'] + 1;
			$data['admin_modify_ip'] = getClientIp();
			$data['admin_modify_date'] = date("Y-m-d H:i:s");
			
			$result = $userModel->where("admin_id='{$userid}'")->save($data);
            if(false !== $result) {
            	$this->success('登陆成功', '__APP__/index');
            }else{
            	$this->error('登陆失败', '__APP__/login/index');
            }
		}else{
			$this->error('登陆失败', '__APP__/login/index');
		}
    }
    //登出系统
    public function logout(){
    	$userModel = M("User");
    	$userid = session('userid');
    	$data['admin_approval_date'] = date("Y-m-d H:i:s");
    	
    	$result = $userModel->where("admin_id='{$userid}'")->save($data);
    	if(false !== $result) {
    		session(null);
			$this->success('登出成功', '__APP__/login/index');
        }else{
        	$this->error('登出失败'.$result, '__APP__/index');
        }
    }
}